function loading(val){
	var $loading = $('#loading');
	if(val)
		$loading.html(val);
	$loading.css('display','block');
}
function test(o){
	alert(o.href);
	return false;
}

$(function($){
	$('body').append('<div id="loading">数据加载中……</div>');
	$('a[href!=#]').click(function(){
		loading();
	});
	/*
	 * ajax 加载select
	 * eg:
	 *	<span style="display:none;" id="goodsClassTemplate">{"maxAjaxNum":4,"action":"list.ajax","paramName":"parentid","key":"clsid","value":"clsname","default":{"key":-1,"value":"--无--"}}</span>
	 *	<select templateId="goodsClassTemplate" id="goodsClass_0" class="_input ajaxSelect">
	 *	</select>
	 *	<input type="hidden" id="parentid" name="parentid" value="0" />
	 */
	var ajaxSelectNameSpace = {};//{NameSpace:{template:{},currentIdSuffix:0,idPrefix:''}}
	$('.ajaxSelect').live('change',function(){
		var _this = this;
		var $this = $(this);
		var templateId = $this.attr('templateId');
		var cache = ajaxSelectNameSpace[templateId];
		var template ;
		var currentIdSuffix ;
		var idPrefix ;
		var idSuffix = _this.id.substring(_this.id.indexOf('_')+1);
		if(cache){
			template = cache['template'];
			currentIdSuffix = cache['currentIdSuffix'];
			idPrefix = cache['idPrefix'];
		}else{
			template = jQuery.parseJSON($('#'+templateId).html());
			currentIdSuffix = 0;
			idPrefix = this.id.substring(0,this.id.indexOf('_')+1);
			ajaxSelectNameSpace[templateId] = {};
			ajaxSelectNameSpace[templateId]['template'] = template;
			ajaxSelectNameSpace[templateId]['currentIdSuffix'] = currentIdSuffix;
			ajaxSelectNameSpace[templateId]['idPrefix'] = idPrefix;
		}
		var paramName = template['paramName'];
		var paramVal = $this.val();
		var defaultVal = template['default'];
		var replaceAfter = function(html){
			/*
			var maxAjaxNum = template['maxAjaxNum'];
			for(;maxAjaxNum>idSuffix;maxAjaxNum--){
				$('#'+idPrefix+maxAjaxNum).remove();
			}
			ajaxSelectNameSpace[templateId]['currentIdSuffix'] = idSuffix;
			*/
			var $selects = $this.nextAll('select');
			if($selects.size() > 0){
				$this.nextAll('select').remove();
			}
			if(html)
				$this.after(html);
			$('#'+paramName).val(_this.value);
		}
		var beginFun = template['beginFun'];
		var endFun = template['endFun'];
		if(!defaultVal || (defaultVal && $this.find(':selected').html() != defaultVal['value'])){
			if(beginFun) window[beginFun](_this);
			var action = template['action']+'?'+paramName+'='+paramVal;
			$.post(action,{},function(data){
				$('#'+paramName).val(paramVal);//form赋值
				var isGo = '';currentIdSuffix = ++idSuffix;
				if(currentIdSuffix < template['maxAjaxNum']){
					isGo = 'ajaxSelect';
				}
				var html = '';
				if(defaultVal || data.length > 0){
					idSuffix--;
					ajaxSelectNameSpace[templateId]['currentIdSuffix'] = currentIdSuffix;
					html = '<select templateId="'+templateId+'" id="'+idPrefix+currentIdSuffix+'" class="_input '+isGo+'">';
					if(defaultVal)
						html += '<option value="'+paramVal+'">'+defaultVal['value']+'</option>';
					var item,other;
					for(var i in data){
						item = data[i];
						other = item[template['otherKey']];
						other = other ? 'other='+other : '';
						html += '<option value="'+item[template['key']]+'" '+other+'>'+item[template['value']]+'</option>';
					}
					html +='</select>';
				}
				replaceAfter(html);
				if(endFun) window[endFun](_this,data);
			},'json');
		}else{
			replaceAfter();
		}
	});
	var op_ajax = {
		_delete:{
			bCall:function(_this){//begin
				return confirm('您确定要删除此项吗？');
			},
			eCall:function(data,_this){//end
				var state = data.state;
				var message = data.message;
				var color = 'red';
				if(state == 0){
					message = message?message:'删除失败~';
				}else if(state == -1){
					message = message?message:'删除失败~服务器忙~';
				}else{
					$(_this).parents('tr').remove();
					message = message?message:'删除成功。';
					color = 'green';
				}
				$.fn.mBox({message:message,fontcolor:color});
			}
		}
	};
	
	//ajax操作
	$('.op_ajax_s').bind('click',function(){
		var $form = $(this).parents('form');
		var action = $form[0].action;
		$form.submit(function(){
			$.post(action,$form.serializeArray(),function(result){
				if(result.state){
					$.fn.mBox({message:result.message,fontcolor:'green'});
				}else{
					$.fn.mBox({message:result.message,fontcolor:'red'});
				}
				if(plugor.endsWith(action,'add')){
					$form[0].reset();
				}else if(plugor.endsWith(action,'edit')){
					window.location.reload();
				}
			},'json');
			return false;
		});
	});
	$('.op_ajax').live('click',function(){
		var _this = this;
		var _delete = false,_add = false,_edit = false;
		var tagName = this.tagName.toLowerCase();
		var uri = '#';
		if(tagName == 'a')
			uri = this.href;
		else
			uri = this.getAttribute('uri');
		if(uri && uri != '#' && !plugor.startsWith(uri,'javascript')){
			var beforeCall = plugor.getParameter('bCall',uri);//开始前调用方法
			var endCall = plugor.getParameter('eCall',uri);//结束后调用方法
			//uri = site_url+uri;
			$.ajax({
				url:uri,
				dataType:'json',
				beforeSend:function(xhr){
					if(beforeCall)
						return window[beforeCall](_this);
					else if(uri.indexOf('/dodelete/') > -1){
						_delete = true;
						return op_ajax['_delete']['bCall'](_this);
					}else if(uri.indexOf('/doadd') > -1){
						_add = true;
//						return op_ajax['_add']['bCall'](_this);
						return;
					}else if(uri.indexOf('/doedit/') > -1){
						_edit = true;
//						return op_ajax['_edit']['bCall'](_this);
						return;
					}
				},
				success:function(data){
					if(endCall)
						window[endCall](_this);
					else if(_delete)
						return op_ajax['_delete']['eCall'](data,_this);
				},
				error:function(){
					alert('错误的URI');
				}
			});
		}
		$('#loading').css('display','none');
		return false;
	});
	
	//批量删除
	$('.op_batch_delete').click(function(){
		var _this = this;
		var $this = $(this);
		var checkName = $this.attr('check');
		if(!checkName)
			checkName = 'checkOne';
		var arr = checkAll.check(checkName);
		if(arr){
			$.fn.mBox({type:'confirm',message:'确认要执行删除操作！',callback:function(r){
				if(r)
					$.post(_this.href+'?ids='+arr,function(data){
						if(data.state == 0){
							alert(data.message);
						}else if(data.state == -1){
							alert(data.message);
						}else{
							window.location.href=data.data+'?message='+data.message;
						}
					},'json');
			}});
		}
		$('#loading').css('display','none');
		return false;
	});
});